import time

from selenium.webdriver.common.by import By


from Driver.Driver import MDriver

class PostDetail:
    driver=''
    def __init__(self):
        self.driver=MDriver.driver
    # 方便写代码
    def QFind(self,name):
        self.driver.find_element(By.CSS_SELECTOR,name)
    # 界面元素检测
    # 共同元素
    def PostDetailFaceSame(self):
        self.driver.execute_script("document.body.style.zoom='0.5'")
        # 标题
        self.QFind("#bit-forum-content > div.page-header.d-print-none > div > div.row.g-2.align-items-center > div")
        # 时间、观看量、点赞数、评论数
        self.QFind("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")
        self.QFind("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)")
        self.QFind("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")
        self.QFind("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4)")
        # 头像
        self.QFind("#article_details_author_avatar")
        # 作者名
        self.QFind("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-3.card > div > h3")
        # 最新回复
        self.QFind("#bit-forum-content > div.page-body > div > div > div:nth-child(2)")
        # 点赞按钮
        self.QFind("#details_btn_like_count")
        # 评论
        # 如果没有评论，检测默认文字
        time.sleep(0.2)
        if self.driver.find_element(By.CSS_SELECTOR,"#details_article_replyCount").text=='0':
            self.QFind("#details_reply_area > p")
            # 如果有评论
        else:
            # 评论者头像
            self.QFind("#details_reply_area > div:nth-child(1) > div.col-3.card > div > span")
            # 评论者名字
            self.QFind("#details_reply_area > div:nth-child(1) > div.col-3.card > div > h3")
            # 评论者回复内容
            self.QFind("#details_reply_area > div:nth-child(1) > div.col-9.card.card-lg > div.card-body")
            # 评论时间
            self.QFind("#details_reply_area > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto > div > div > ul > li")
        # 评论编辑器
        self.QFind("#article_details_reply")
        # 回复按钮
        self.QFind("#details_btn_article_reply")
    # 别人帖子元素
    def PostDetailFaceOther(self):
        # 给作者发私信按钮
        self.QFind("#btn_details_send_message")
    # 点赞功能
    def PostDetailLike(self):
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count").click()
    # 给作者发私信
    def PostDetailSendToAuthor(self,name):
        self.driver.find_element(By.CSS_SELECTOR,"#btn_details_send_message").click()
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_content").send_keys(name)
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#btn_index_send_message").click()
    # 发送评论
    def PostDetailSendComment(self,name):
        self.driver.execute_script("arguments[0].scrollIntoView();",self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply"))
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(39) > a > i").click()
        if  name is None:
            self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply").click()
        else:
            self.driver.find_element(By.CSS_SELECTOR,"#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(4) > a > i").click()
            self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply").click()
